#!/usr/bin/env node

const EventEmitter = require('events').EventEmitter;

function MusicPlayer(track) {
    this.track = track;
    this.playing = false;

    for(const methodName in EventEmitter.prototype) {
        console.log(methodName);
        this[methodName] = EventEmitter.prototype[methodName];
    }
}

MusicPlayer.prototype = {
    toString: function() {
        if(this.playing) {
            return "Now Playing: " + this.track;
        }else {
            return 'Stopped' ;
        }
    } 
};



const musicPlayer = new MusicPlayer('storm');

musicPlayer.on('play', function() {
    this.playing =  true;
    console.log('\n', true.toString());
});

musicPlayer.on('stop', function() {
    this.playing = false;
    console.log('\n', this.toString());
});


//整个程序上捕获异常
process.on('uncaughtException', function(err) {
    console.log(err);
    process.exit(1);
})

//没有出现异常，不出现
// musicPlayer.on('error', function(err) {
//     console.log(err);
//     process.exit(1);
// })

musicPlayer.emit('play');

setTimeout(function() {
    musicPlayer.emit('stop');
},2000);

//自己加的异常
setTimeout(function() {
    musicPlayer.emit('error', new Error('WRONG!'));
},3000);